package com.tangdi.production.mpaychl.service.impl.wxpay.utils;


import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class XMLUtil {

    /**
     * 解析xml,返回第一级元素键值对。如果第一级元素有子节点，则此节点的值是子节点的xml数据。
     *
     * @param strxml
     * @return
     * @throws JDOMException
     * @throws IOException
     */
    public static Map doXMLParse(String strxml) throws JDOMException, IOException {
        strxml = strxml.replaceFirst("encoding=\".*\"", "encoding=\"UTF-8\"");

        if (null == strxml || "".equals(strxml)) {
            return null;
        }

        Map m = new HashMap();

        InputStream in = new ByteArrayInputStream(strxml.getBytes("UTF-8"));
        SAXBuilder builder = new SAXBuilder();
        Document doc = builder.build(in);
        Element root = doc.getRootElement();
        List list = root.getChildren();
        Iterator it = list.iterator();
        while (it.hasNext()) {
            Element e = (Element) it.next();
            String k = e.getName();
            String v = "";
            List children = e.getChildren();
            if (children.isEmpty()) {
                v = e.getTextNormalize();
            } else {
                v = XMLUtil.getChildrenText(children);
            }

            m.put(k, v);
        }

        // 关闭流
        in.close();

        return m;
    }

    public static void main(String[] args) throws JDOMException, IOException {

        String xmlStr="<xml><appid>wx0d99991d82e42ff9</appid><body><![CDATA[充值]]></body><mch_id>1593649411</mch_id><nonce_str>0002511751</nonce_str><notify_url>http://gozike.newiba.com:8083/mpcctp/H5-wxpay/notify-url.json</notify_url><out_trade_no>20200715000251668019</out_trade_no><scene_info>{\"h5_info\": {\"type\":\"Wap\",\"wap_url\": \"www.newiba.com\",\"wap_name\": \"商品交易\"}}</scene_info><sign><![CDATA[9A15E80D48B9D7FD30F836B4237534A0]]></sign><sign_type>MD5</sign_type><spbill_create_ip>154.209.249.67</spbill_create_ip><total_fee>3000</total_fee><trade_type>MWEB</trade_type></xml>";

        Map map = XMLUtil.doXMLParse(xmlStr);

        for (Object key : map.keySet()) {

            System.out.println(key + ":" + map.get(key));

        }

    }

    /**
     * 获取子结点的xml
     *
     * @param children
     * @return String
     */
    public static String getChildrenText(List children) {
        StringBuffer sb = new StringBuffer();
        if (!children.isEmpty()) {
            Iterator it = children.iterator();
            while (it.hasNext()) {
                Element e = (Element) it.next();
                String name = e.getName();
                String value = e.getTextNormalize();
                List list = e.getChildren();
                sb.append("<" + name + ">");
                if (!list.isEmpty()) {
                    sb.append(XMLUtil.getChildrenText(list));
                }
                sb.append(value);
                sb.append("</" + name + ">");
            }
        }
        return sb.toString();
    }
}
